package com.sec.android.app.myfiles.launch;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.cloud.CloudFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import java.util.ArrayList;

public class HandleStartFolderImp extends AbsHandleLaunchImp {

    @Override
    public boolean isMyJob(Intent intent) {
        boolean bRet = false;
        final String action = intent.getAction();
        if (Intent.ACTION_MAIN.equals(action) || AppConstants.Action.ACTION_LAUNCH_MY_FILES.equals(action)) {
            String path = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_START_PATH);
            bRet = !TextUtils.isEmpty(path);
        }
        return bRet;
    }

    @Override
    public boolean _handleStart(int processId, Activity activity, Intent intent) {
        String path = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_START_PATH);
        Bundle cloudInfo = intent.getBundleExtra(AppConstants.MyFilesExtras.EXTRA_START_CLOUD_INFO);

        if (cloudInfo == null) {
            if (intent.getBooleanExtra(AppConstants.MyFilesExtras.START_FROM_NOTIFICATION, false)) {
                Log.i(this, "My Files opened from notification - remove notification of MyFiles");
                NotificationManager notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(0);
            }
            NavigationManager naviMgr = NavigationManager.getInstance(processId);
            naviMgr.clearCurNavigationStack();
            return executePath(processId, activity, path);
        } else {
            NavigationManager naviMgr = NavigationManager.getInstance(processId);
            naviMgr.clearCurNavigationStack();
            return executeCloudFolder(processId, activity, cloudInfo, path);
        }
    }

    protected boolean executePath(int processId, Activity activity, String path) {
        boolean ret = false;
        if (!TextUtils.isEmpty(path)) {
            FileRecord record = FileRecord.createFileRecord(FileRecord.StorageType.Local, path);
            if (record != null) {
                if (record.exists(activity)) {
                    MyFilesFacade.executeRecord(processId, activity, record);
                } else {
                    Log.e(this, "executePath - not exist:" + Log.getEncodedMsg(path));
                    Toast.makeText(activity, R.string.invalid_resource_path_specified, Toast.LENGTH_SHORT).show();
                    MyFilesFacade.goHome(processId, null, activity);
                }
                ret = true;
            }
        }
        return ret;
    }

    protected boolean executeCloudFolder(int processId, Activity activity, Bundle cloudInfoBundle, String path) {
        boolean ret = false;
        if (cloudInfoBundle != null) {
            boolean isStartFromSamsungCloud = AppConstants.MyFilesExtras.EXTRA_START_CLOUD_FROM_SAMSUNG_CLOUD.equals(cloudInfoBundle.getString(AppConstants.MyFilesExtras.EXTRA_START_CLOUD_FROM));

            CloudFileRecord record;
            if (isStartFromSamsungCloud) {
                record = (CloudFileRecord) FileRecord.createFileRecord(FileRecord.StorageType.Cloud, AppConstants.StoragePath.SAMSUNG_DRIVE_FOLDER);
                if (record != null) {
                    record.setFileType(FileType.FOLDER);
                    record.setFileId(CloudFileRecord.CLOUD_ROOT);
                    record.setDeviceId(CloudMgr.getInstance(activity).getDeviceId(record.getCloudType()));
                }
            } else {
                int deviceId = cloudInfoBundle.getInt(AppConstants.MyFilesExtras.EXTRA_CLOUD_DEVICEID);
                String fileId = cloudInfoBundle.getString(AppConstants.MyFilesExtras.EXTRA_CLOUD_FILEID);
                ArrayList<String> parentIds = cloudInfoBundle.getStringArrayList(AppConstants.MyFilesExtras.EXTRA_CLOUD_PARENTID);
                record = new CloudFileRecord(deviceId, null, fileId, path, true);
                if (record != null) {
                    record.setParentIds(parentIds);
                }
            }

            if (record != null) {
                if (record.exists(activity)) {
                    MyFilesFacade.executeRecord(processId, activity, record);
                } else {
                    if (!isStartFromSamsungCloud) {
                        Toast.makeText(activity, R.string.invalid_resource_path_specified, Toast.LENGTH_SHORT).show();
                    }
                    MyFilesFacade.goHome(processId, null, activity);
                }
                ret = true;
            }
        }
        return ret;
    }
}
